package com.airport.schedule.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat dayPrefixFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static long ONE_DAY = 24 * 3600 * 1000;

    public static String format(long time) {
        return sdf.format(time*1000);
    }

    public static boolean isStayOverNightFlight(String arrivalTime, String departureTime) {
        Long a = Long.valueOf(arrivalTime);
        Long d = Long.valueOf(departureTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(a);
        int arrivalHour = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.clear();
        calendar.setTimeInMillis(d);
        int departureHour = calendar.get(Calendar.HOUR_OF_DAY);
        return d - a < ONE_DAY && arrivalHour > 21 && departureHour <= 9;
    }

    public static String getPrefix(long time) {
        return dayPrefixFormat.format(time * 1000);
    }

    public static long getTime(String date) {
        try {
            return dayPrefixFormat.parse(date).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        throw new IllegalStateException("无法解析日期："+date);
    }

}
